<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<input type="text" placeholder="请输入用户名" id="name">
<input type="password" placeholder="请输入用密码" id="pwd">
<button id="btn" onclick="login()">登录</button>


<script>
    function login(){
        const name = document.getElementById("name").value //获取id=name的输入框信息
        const pwd = document.getElementById("pwd").value//获取id = pwd的输入框信息

        if (!name || !pwd){
            alert("请输入用户名或密码")
        }
        fetch('/login',{
            method: "POST",
            headers:{
                'Content-Type': 'application/json;charset=UTF-8'
            },
            body: JSON.stringify({name: name,password: pwd})
        }).then(res => res.text()).then(res=>{
            console.log(res)
            if (res){
                let json =JSON.parse(res) //将数据变成json格式
                console.log(json)
                if (json.code === 1){
                    window.location.href='/'   //登录成功后跳转到首页
                }
            }else {
                alert("账号或密码错误")
            }

        })
    }



</script>

</body>
</html>